package com.roiland.contentproviderdemo;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * Created by wenhui.li on 2015/8/27.
 */
public class ProfileContentProvider extends ContentProvider {

    DBHelper mDbHelper = null;
    SQLiteDatabase db = null;

    private static final UriMatcher mMatcher;

    static {
        mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mMatcher.addURI(ContentConstants.AUTOHORITY, ContentConstants.TABLE_NAME, ContentConstants.ITEM);
        mMatcher.addURI(ContentConstants.AUTOHORITY, ContentConstants.TABLE_NAME + "/#", ContentConstants.ITEM_ID);
    }

    @Override
    public boolean onCreate() {
        mDbHelper = new DBHelper(getContext());
        db = mDbHelper.getReadableDatabase();
        return true;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long rowId;
        if (mMatcher.match(uri) != ContentConstants.ITEM) {
            throw new IllegalArgumentException("Unknown URI" + uri);
        }
        rowId = db.insert(ContentConstants.TABLE_NAME, null, values);
        if (rowId > 0) {
            Uri noteUri = ContentUris.withAppendedId(ContentConstants.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }

        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        switch (mMatcher.match(uri)) {
            case ContentConstants.ITEM:
                return db.delete(ContentConstants.TABLE_NAME, selection, selectionArgs);
            case ContentConstants.ITEM_ID:
                long id = ContentUris.parseId(uri);
                String where = ContentConstants.COLUMN_ID + "=" + id;
                if (selection != null && !"".equals(selection)) {
                    where = where + " and " + selection;
                }
                return db.delete(ContentConstants.TABLE_NAME, where, selectionArgs);
            default:
                throw new IllegalArgumentException("Unknown Uri : " + uri);
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        switch (mMatcher.match(uri)) {
            case ContentConstants.ITEM:
                return db.update(ContentConstants.TABLE_NAME, values,selection,
                        selectionArgs);
            case ContentConstants.ITEM_ID:
                long id = ContentUris.parseId(uri);
                String where = ContentConstants.COLUMN_ID + "=" + id;
                if (selection != null && !"".equals(selection)) {
                    where = where + " and " + selection;
                }
                return db.update(ContentConstants.TABLE_NAME, values, where,
                        selectionArgs);
            default:
                throw new IllegalArgumentException("Unknown Uri : " + uri);
        }
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                        String sortOrder) {
        // TODO Auto-generated method stub
        Cursor c = null;
        switch (mMatcher.match(uri)) {
            case ContentConstants.ITEM:
                c = db.query(ContentConstants.TABLE_NAME, projection, selection, selectionArgs,
                        null, null, sortOrder);
                break;
            case ContentConstants.ITEM_ID:
                c = db.query(ContentConstants.TABLE_NAME, projection, ContentConstants.COLUMN_ID + "=" + uri.getLastPathSegment(),
                        selectionArgs, null, null, sortOrder);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        }

        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public String getType(Uri uri) {
        switch (mMatcher.match(uri)) {
            case ContentConstants.ITEM:
                return ContentConstants.CONTENT_TYPE;
            case ContentConstants.ITEM_ID:
                return ContentConstants.CONTENT_ITEM_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        }
    }
}
